import Passport from 'koa-passport';
import LocalStrategy from 'passport-local';
import UserModel from './../../models/users';
// 用户验证
Passport.use(new LocalStrategy(async function(username, password, done){
  const user = {username: username};
  const res = await UserModel.findOne(user);
  if (res !== null) {
    if (res.password === password) {
      return done(null, res);
    } else {
      return done(null, false, '密码错误');
    }
  } else {
    return done(null,false, '用户名不存在');
  }
}));

Passport.serializeUser(function(user, done){
  done(null, user);
})
Passport.deserializeUser(function(user, done) {
  done(null, user);
})

export default Passport